package cn.windy.samary.utils;

import cn.windy.samary.module.admin.common.UserLine;
import cn.windy.samary.module.admin.entity.User;
import cn.windy.samary.module.admin.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Collection;
import java.util.List;

public class ShiroUtils {

    @Autowired
    private static EnterpriseCacheSessionDAO sessionDAO;

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static Session getSession() {
        return getSubject().getSession();
    }

    public static User getUser() {
        UserMapper userMapper = (UserMapper) ApplicationContextUtil.getBean("userMapper");
        return userMapper.selectByPrimaryKey(getUserId());
    }

    public static UserLine getUserLine() {
        return (UserLine) getSubject().getPrincipal();
    }

    public static String getUserId() {
        UserLine userLine = (UserLine) getSubject().getPrincipal();
        return userLine.getId();
    }

    public static void logout() {
        getSubject().logout();
    }

    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) ((WebSubject) getSubject()).getServletRequest();
    }

    public static List<Principal> getPrinciples() {
        List<Principal> principals = null;
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        return principals;
    }

}
